<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      @keyup.enter.native="getDataList()">
      <el-form-item label="订单号">
        <el-input clearable style="width: 240px" v-model="queryParams.search.orderNo" placeholder="请输入订单号"
          clearable></el-input>
      </el-form-item>
      <el-form-item v-if="sysUser.isAdmin" label="代理商">
        <el-select v-model="queryParams.search.agentId" placeholder="请选择代理商" clearable style="width: 240px">
          <el-option v-for="dict in franchiseeList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">

        <el-select v-model="queryParams.search.status" placeholder="订单状态" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.order_status" :key="Number(dict.value)" :label="dict.label"
            :value="Number(dict.value)" />
        </el-select>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="date">
        <el-date-picker v-model="dataDate" type="daterange" range-separator="至" start-placeholder="开始日期" size="mini"
          value-format="yyyy-MM-dd" @change="onDateChange" end-placeholder="结束日期">
        </el-date-picker>
        <el-form-item>
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" v-loading="loading" style="width: 100%;">

      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="orderNumber" label="订单编号" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_pay_status" :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付类型" align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_pay_type" :value="scope.row.settlement" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="下单时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="meetAmount" label="应付金额" align="right" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.meetAmount.toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column prop="paidAmount" label="实付金额" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.paidAmount.toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column prop="agentName" label="代理商" align="left" :show-overflow-tooltip="true" />
      <el-table-column label="操作" fixed="right"  width="250">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:order:list']">修改</el-button> -->
          <el-link type="primary" icon="el-icon-truck" @click="sendGoods(scope.row)" style="margin-left:10px" v-if="scope.row.status == 20">发货</el-link>
          <el-link type="primary" icon="el-icon-view" @click="preview(scope.row)" style="margin-left:10px" >查看</el-link>
          <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" style="margin-left:10px" >删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDataList" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>
<script>
  import {
    mapGetters
  } from 'vuex'
  import {
    page,
    deleteById,
    send,
    getSales
  } from "@/api/business/orderinfo";
  import AddOrUpdate from './addOrUpdate'
  import {
    page as getAllFranchisee
  } from "@/api/business/franchiseeinfo";
  export default {
    computed: {
      ...mapGetters(["sysUser"]),
    },
    dicts: ['order_status', 'order_pay_status','order_pay_type'],
    data() {
      return {
        franchiseeList: [],
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        // 总条数
        total: 0,
        //表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 表单参数
        // 显示搜索条件
        showSearch: true,
        order_status: [{
            label: '未支付',
            value: '10'
          },
          {
            label: '支付成功（待发货）',
            value: '20'
          },
          {
            label: '待收货',
            value: '30'
          },
          {
            label: '已完成',
            value: '40'
          },
          {
            label: '已取消',
            value: '50'
          },
        ],
        form: {},
        // 日期范围
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          search: {
            keyword: '',
            agentId: ''
          }
        },
        dataList: [],
        rules: {},
        addOrUpdateVisible: false,
        dataDate: [],
      }
    },
    components: {
      AddOrUpdate
    },
    created() {
      this.getDataList();
      this.initPage();
    },
    methods: {
      preview(row) {
        this.addOrUpdateVisible = true;
        this.title = "添加";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },
      sendGoods(e) {
        this.$modal.confirm('确定要发货吗？').then(function() {
          return send([e]);
        }).then(() => {
          this.getDataList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
      },
      onDateChange(e) {
        const search = this.queryParams.search;
        search.startDate = e[0];
        search.endDate = e[1];
        this.queryParams.search = search;
      },
      initPage() {
        getAllFranchisee({
          pageNum: 0,
          pageSize: 100000,
          search: {
            status: 1
          }
        }).then(res => {
          this.franchiseeList = res.data.list
        })
      },
      // 获取数据列表
      getDataList() {
        this.loading = true;
        page(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          console.log('response', response)
          this.dataList = response.data.list;
          this.total = parseInt(response.data.total);
          this.loading = false;
        }).catch(res => {
          this.loading = false
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getDataList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('确定要删除选中的数据项？').then(function() {
          return deleteById(ids);
        }).then(() => {
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      resetQuery() {
        this.dataDate = [];
        this.resetForm("queryForm");
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          search: {
            keyword: '',
            agentId: ''
          }
        };
        this.getDataList()
      }
    }
  }
</script>
